import type { ReactNode } from 'react'
import { Navigate, useLocation } from 'react-router-dom'
import { useAuth } from '@store/AuthProvider'
import { ROUTES } from './paths'

type PublicRouteProps = {
  children: ReactNode
}

const PublicRoute = ({ children }: PublicRouteProps) => {
  const location = useLocation()
  const { user } = useAuth()

  if (user) {
    const redirect =
      (location.state as { from?: string } | undefined)?.from || ROUTES.dashboard
    return <Navigate to={redirect} replace />
  }

  return <>{children}</>
}

export default PublicRoute


